package com.banciyuan.bcywebview.biz.main.mineinfo.reward;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.api.GiftApi;
import com.banciyuan.bcywebview.base.fragment.BaseFragment;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.view.dialog.WaitDialog;
import com.banciyuan.bcywebview.biz.web.WebActivity;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;

import com.banciyuan.bcywebview.utils.http.BcyPair;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by squall on 2016/5/27.
 */
public class OpenRewardFragment extends BaseFragment {

    private ImageView web_png;
    private TextView web_notice;
    private CheckBox cb_read;
    private TextView comfirm_bt;
    private TextView comfirm_disable_bt;
    private RequestQueue mQueue;
    private WaitDialog waitDialog;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.openreward_fragment, null);
        initArgs();
        initUi(view);
        return view;
    }

    @Override
    protected void initArgs() {
        mQueue = VolleyQueue.getRquest(getActivity());
    }

    @Override
    protected void initUi(View rootview) {
        web_png = (ImageView) rootview.findViewById(R.id.web_png);
        web_notice = (TextView) rootview.findViewById(R.id.arrgement_tv);
        comfirm_bt = (TextView) rootview.findViewById(R.id.feed_now);
        comfirm_disable_bt = (TextView) rootview.findViewById(R.id.feed_now_disable);
        cb_read = (CheckBox) rootview.findViewById(R.id.check_cb);
        waitDialog = new WaitDialog(getActivity(), R.style.Dialog);
        ImageLoader.getInstance().displayImage(getString(R.string.open_award_pic), web_png);

        cb_read.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    comfirm_disable_bt.setVisibility(View.GONE);
                    comfirm_bt.setVisibility(View.VISIBLE);
                } else {
                    comfirm_bt.setVisibility(View.GONE);
                    comfirm_disable_bt.setVisibility(View.VISIBLE);
                }
            }
        });

        web_notice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoUtil.gotoAct(getActivity(), WebActivity.class, getString(R.string.giftagree));
            }
        });

        web_png.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoUtil.gotoAct(getActivity(), WebActivity.class, getString(R.string.giftagree));
            }
        });

        comfirm_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!waitDialog.isShowing()) {
                    waitDialog.show();
                    open();
                }
            }
        });
    }

    private void open() {
        String url = HttpUtils.BASE_URL + GiftApi.getOpenPayment();
        List<BcyPair> pairs = new ArrayList<>();
        pairs.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(getActivity()).getToken()));
        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (StatusCodeUtils.dealStatusCode(response, getActivity())) {
                    if (OpenRewardFragment.this.isAdded()) {
                        MyRewardActivity activity = (MyRewardActivity) getActivity();
                        if (activity != null && !activity.isFinishing()) {
                            Intent intent = new Intent(getActivity(), EditRewardActivity.class);
                            activity.startActivityForResult(intent, MyRewardActivity.GO_OPEN_REWARD);
                            activity.overridePendingTransition(R.anim.umeng_socialize_fade_in, R.anim.umeng_socialize_fade_out);
                        }
                    }
                }
                waitDialog.dismiss();
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                waitDialog.dismiss();
            }
        };

        Map<String, String> map = HttpUtils.getData(pairs);
        StringRequestParam stringRequestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, onError);
        mQueue.add(stringRequestParam);
    }
}
